package cn.itcast.io.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.TreeSet;

public class StorageStudentInfo {

    private static final Object LINE_SEPARATOR = System.getProperty("line.separator");

    public static void main(String[] args) throws IOException {

        TreeSet<Student> students = new TreeSet<>(Collections.reverseOrder());
        
        
        students.add(new Student("张三", 60, 80, 100));
        students.add(new Student("李四", 10, 20, 40));
        students.add(new Student("王五", 50, 90, 80));
        students.add(new Student("赵六", 70, 50, 90));
        
        for(Student student : students){
            System.out.println(student.toString());
        }
        File file = new File("tempFile");
        File destFile = new File(file, "student_info.txt");
        write2File(students,destFile);
    }

    private static void write2File(TreeSet<Student> students, File destFile) throws IOException {

        FileOutputStream fos = null;

        for (Student student : students){
            fos =  new FileOutputStream(destFile, true);
            String studentInfo = student.getStudentName()+"\t"+student.getSum()+LINE_SEPARATOR;
            fos.write(studentInfo.getBytes());
        }
        fos.close();
    }
}
